package com.leetcode;

/**
 * 868. 二进制间距
 *
 * @author fy
 * @date 2022/4/24 11:37
 */
public class Solution868 {

    public int binaryGap(int n) {
        String bStr = Integer.toBinaryString(n);
        int p = bStr.length();
        int q = 0;
        int res = 0;
        while (q < bStr.length()) {
            if (bStr.charAt(q) == '0') {
                q++;
                continue;
            }
            if (bStr.charAt(q) == '1') {
                res = Math.max(res, q - p);
                p = q;
                q++;
            }
        }
        return res;
    }

}
